<?php

namespace tools\lib\main\model;

use tools\lib\main\Common;

/**
 * Class Constant
 *
 * 视为常量的条件: 字段类型:tinyint 字段描述: 状态  0 隐藏 hide 1 显示 show
 *
 * @package tools\lib\main\model
 */
class Constant extends Common
{
    /**
     * [
     *  [const,value,comment],
     *  [const,value,comment],
     *  [const,value,comment],
     * ]
     *
     * 测试值
     * $this->constant = [
     *  ['status_zero', 0, 'zero'],
     *  ['status_one', 1, 'one'],
     *  ['status_two', 2, 'two'],
     *  ['type_agent', 1, '经销商'],
     *  ['type_sale', 2, '销售员'],
     * ];
     *
     * @var array
     */
    private $constant = [];

    /**
     * @return string
     */
    public function template()
    {

        $constants = $this->transConstant();

        if (empty($constants)) {
            return null;
        }

        $template = <<<EOT
<?php

{$this->getFileNamespace()}

class {$this->className()} 
{
   $constants
}
EOT;
        return $template;
    }

    /**
     * @param $constant
     */
    public function setConstant($constant)
    {
        $this->constant = $constant;
    }

    /**
     * @return array
     */
    public function getConstant()
    {
        return $this->constant;
    }

    /**
     * @return bool|string
     */
    private function transConstant()
    {
        $this->constant = $this->constant ? $this->constant : $this->findConstantFromModel();

        if (empty($this->constant)) {
            return '';
        }

        $template = '' . "\n";

        foreach ($this->constant as $item) {

            if (!is_array($item)) {
                dump('create constant fail: options is not array');
                return false;
            }

            if (count($item) != 3) {
                dump('create constant fail: options params short');
                return false;
            }

            if (!$variable = $item[0]) {
                return false;
            };

            $variable = strtoupper($variable);
            $value = $item[1] ? $item[1] : 0;
            $comment = $item[2] ? $item[2] : '';

            $template .= <<<ETO
    /**
     * $comment
     * @var int
     */
    const $variable = $value;
ETO;
            $template .= "\n\n";
        }

        $template = rtrim($template, "\n");

        return $template;
    }

    /**
     * @return array
     */
    private function findConstantFromModel()
    {
        $columns = tableColumns($this->parameter->getName());
        $constant = [];
        if ($columns) {
            foreach ($columns as $column) {
                $field_name = $column['Field'];
                $field_type = $column['Type'];
                $field_comment = $column['Comment'];
                if (preg_match('/tinyint/', $field_type)) {

                    $field_comment = parseFieldComment($field_comment);

                    if ($field_comment) {
                        foreach ($field_comment as $key => $comment) {
                            if (count($comment) != 3 || !preg_match('/^[a-zA-Z]+/', $comment[2])) {
                                break 1;
                            }
                            $description = $comment[1];
                            $value = $comment[0];
                            $name = $comment[2];
                            $constant[] = [
                                // 常量名称
                                $field_name . '_' . $name,
                                // 常量值
                                $value,
                                // 常量备注
                                $description
                            ];
                        }
                    }
                }
            }
        }

        return $constant;
    }
}